home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FILES.SWG / 0042_Close All Files.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  4KB  |  114 lines

  1. {
  2. >>DOS will automatically close all open files that belong to your
  3. >>process upon termination.  The only way I know of to do it manually,
  4. >>if you don't know what the actual file variables will be, is to search
  5. >>your PSP
  6.  
  7. >   I was doing fine until this point, what's a psp?
  8. PSP stands for Program Segment Prefix.  It contains a lot of information
  9. about your program that is important to DOS.  Some of the things it
  10. contains is the file handle table for open files, the command line tail,
  11. information carried over from CP/M, an ISR table, a pointer to a copy of
  12. the master environment, and more.
  13.  
  14. >for any open file handles, then explicitly call DOS, passing
  15. >>each file handle number. If nobody has a better suggestion, I could
  16. >>probably think up some code to do that.
  17.  
  18. >   at least psuedo code would be appreciated..
  19. Here is some code that will close all of the files that your program
  20. actually opened.  It won't clear the run-time error; you'll have to
  21. put that code into the exit-proc, or write your own.
  22.  
  23. { FCLOSALL.PAS
  24.   file close unit
  25.   12-5-93
  26.   (c) 1993 Brian Pape
  27.  
  28.   This code may be distributed freely.  However, I would appreciate it
  29.   if modifications made to the code would be noted with the name of the
  30.   modifier and the date.
  31.  
  32.   This program will demonstrate how to close all open files in
  33.   your program without knowing what the names of the associated
  34.   file variables are.  All that you need to do in order to implement
  35.   this code is put the statement USES FCLOSALL in your main program.
  36.  
  37.   When your program ends, whether through a run-time error or through
  38.   normal termination, this procedure will attempt to close all open files
  39.   that were opened by your program.  It will not close the standard i/o
  40.   file handles that are maintained by DOS.  In fact, the Turbo RTL will
  41.   automatically close the INPUT and OUTPUT standard files in the standard
  42.   exit procedure.  The other DOS standard I/O files are StdErr, AUX,
  43.   and PRN.
  44.  
  45.   This code does not clear the ExitCode variable, so if your program is
  46.   terminating with a run-time error, the turbo ExitProc will still
  47.   print the "Runtime Error at xxxx:xxxx" message.  If you want to
  48.   prevent this message from occuring, then write another exitproc to
  49.   clear the ExitCode variable in certain cases.
  50.  
  51.   This code requires TP 6.0 or greater since it uses BASM
  52. }
  53.  
  54. unit fclosall;
  55. interface
  56. implementation
  57. var
  58.   saveexit:pointer;
  59.  
  60. procedure close_files_exit_proc; far;
  61. var
  62.   numhandles : byte;
  63.   hp : ^byte;
  64. begin
  65.  
  66.   exitproc := saveexit;
  67.  
  68.   { get number of file handles available }
  69.   numhandles := byte(ptr(prefixseg,$32)^);
  70.  
  71.   { get the location of the fht, in case it is moved }
  72.   hp := pointer(ptr(prefixseg,$34)^);
  73.   inc(hp,5);
  74.  
  75.   { skip the first 5 handles because they are standard DOS handles }
  76.   for numhandles := 5 to pred(numhandles) do
  77.     begin
  78.       asm
  79.         mov  ah,3eh
  80.         xor  bh,bh
  81.         push ds
  82.         lds  si,hp
  83.         mov  bl,[si]
  84.         cmp  bl,0ffh  { don't close invalid handle; it will close INPUT }
  85.         je   @invalidhandle
  86.         int  $21
  87.         @invalidhandle:
  88.         pop ds
  89.       end;
  90.       inc(hp);
  91.     end;
  92. end;
  93.  
  94. begin
  95.   saveexit := exitproc;
  96.   exitproc := @close_files_exit_proc;
  97. end.  { FCLOSALL }
  98.  
  99.  
  100. { tests the FCLOSALL unit }
  101. program test_fcloseall;
  102. uses fclosall;
  103. var
  104.   f : file;
  105.   i : byte;
  106. begin
  107.   for i := 1 to 16 do
  108.     begin
  109.       assign(f,'a.a');
  110.       rewrite(f);
  111.     end;  { for }
  112. end.
  113.  
  114.